# coding: utf-8
from typing import Union

from PySide6.QtCore import Qt, QUrl, Signal
from PySide6.QtWidgets import QVBoxLayout, QWidget, QLabel, QPushButton, QHBoxLayout, QGridLayout
from qfluentwidgets import SingleDirectionScrollArea, HyperlinkLabel, CardWidget, FlowLayout

from common import StyleSheet
from ..widgets import WebCentreImageLabel, WebAvatarWidget, CentreImageLabel


class TypeCard(QWidget):
    clicked = Signal()

    def __init__(self, pixmap: str, title: str, body: str, parent=None):
        super().__init__(parent)
        self.verticalLayout = QVBoxLayout(self)
        self.imageLabel = WebCentreImageLabel(self)
        self.titleLabel = QLabel(title, self)
        self.bodyLabel = QLabel(body, self)

        self.titleLabel.setObjectName("titleLabel")
        self.bodyLabel.setObjectName("bodyLabel")
        self.titleLabel.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.bodyLabel.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.verticalLayout.setSpacing(0)
        self.verticalLayout.addWidget(self.imageLabel, 0, Qt.AlignmentFlag.AlignCenter)
        self.verticalLayout.addWidget(self.titleLabel, 0, Qt.AlignmentFlag.AlignCenter)
        self.verticalLayout.addWidget(self.bodyLabel, 0, Qt.AlignmentFlag.AlignCenter)
        self.imageLabel.setBorderRadius(5, 5, 5, 5)
        self.imageLabel.setFixedSize(200, 200)

        self.setPixmap(pixmap)
        self.setFixedSize(225, 280)
        StyleSheet.Card.apply(self)
        self.imageLabel.clicked.connect(self.clicked)

    def setPixmap(self, pixmap: str):
        self.imageLabel.setPixmap(pixmap)

    def setUrl(self, url: Union[str, QUrl], headers: dict = None):
        self.imageLabel.setUrl(url, headers)

    def setTitle(self, title: str):
        self.titleLabel.setText(title)

    def setBody(self, body: str):
        self.bodyLabel.setText(body)

    def leaveEvent(self, a0):
        self.imageLabel.setShadowVisible(False)

    def enterEvent(self, a0):
        self.imageLabel.setShadowVisible(True)


class SingerCard(QWidget):
    clicked = Signal()

    def __init__(self, pixmap: str, singer: str, parent=None):
        super().__init__(parent)
        self.verticalLayout = QVBoxLayout(self)
        self.imageLabel = WebAvatarWidget(self)
        self.singerLabel = QLabel(singer, self)
        self.followButton = QPushButton('关注', self)

        self.singerLabel.setObjectName("singerLabel")
        self.followButton.setObjectName("followButton")
        self.followButton.setFlat(True)
        self.singerLabel.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.verticalLayout.setSpacing(0)
        self.verticalLayout.addWidget(self.imageLabel, 0, Qt.AlignmentFlag.AlignCenter)
        self.verticalLayout.addWidget(self.singerLabel, 0, Qt.AlignmentFlag.AlignCenter)
        self.verticalLayout.addWidget(self.followButton, 0, Qt.AlignmentFlag.AlignCenter)
        self.imageLabel.setFixedSize(200, 200)
        self.imageLabel.setRadius(100)
        self.setPixmap(pixmap)
        self.setFixedSize(220, 280)
        StyleSheet.Card.apply(self)
        self.imageLabel.clicked.connect(self.clicked)

    def setPixmap(self, pixmap: str):
        self.imageLabel.setPixmap(pixmap)

    def setUrl(self, url: Union[str, QUrl], headers: dict = None):
        self.imageLabel.setUrl(url, headers)

    def setSinger(self, singer: str):
        self.singerLabel.setText(singer)

    def leaveEvent(self, a0):
        self.imageLabel.setShadowVisible(False)

    def enterEvent(self, a0):
        self.imageLabel.setShadowVisible(True)


class GridWidgetCard(QWidget):
    def __init__(self, title: str, body: str, parent=None):
        super().__init__(parent)
        self.verticalLayout = QVBoxLayout(self)
        self.cardWidget = CardWidget(self)
        self.gridLayout = QGridLayout(self.cardWidget)
        self.defaultPos = [(0, 0), (0, 1), (1, 0), (1, 1)]
        self.titleLabel = QLabel(title, self)
        self.bodyLabel = HyperlinkLabel(body, self)

        self.cardWidget.setFixedSize(354, 354)
        self.titleLabel.setObjectName("titleLabel")
        self.gridLayout.setSpacing(10)
        self.gridLayout.setContentsMargins(10, 10, 10, 10)
        self.gridLayout.setAlignment(Qt.AlignmentFlag.AlignCenter)

        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.addWidget(self.cardWidget, 0, Qt.AlignmentFlag.AlignCenter)
        self.verticalLayout.addWidget(self.titleLabel, 0, Qt.AlignmentFlag.AlignCenter)
        self.verticalLayout.addWidget(self.bodyLabel, 0, Qt.AlignmentFlag.AlignCenter)
        self.setFixedSize(354, 443)
        StyleSheet.Card.apply(self)

    def addImage(self, pixmap: str):
        count = self.gridLayout.count()
        if count > len(self.defaultPos) - 1:
            return

        pos = self.defaultPos[count]
        label = CentreImageLabel(pixmap, self)
        label.setFixedSize(151, 151)
        label.setBorderRadius(5, 5, 5, 5)
        self.gridLayout.addWidget(label, *pos)

    def addUrlImage(self, url: Union[str, QUrl], headers: dict = None):
        count = self.gridLayout.count()
        if count > len(self.defaultPos) - 1:
            return
        pos = self.defaultPos[count]
        label = WebCentreImageLabel(self)
        label.setUrl(url, headers)
        label.setFixedSize(151, 151)
        label.setBorderRadius(5, 5, 5, 5)
        self.gridLayout.addWidget(label, pos)

    def addImages(self, pixmaps: list):
        for pixmap in pixmaps:
            self.addImage(pixmap)

    def addUrlImages(self, urls: list, headers: dict = None):
        for url in urls:
            self.addUrlImage(url, headers)

    def setTitle(self, title: str):
        self.titleLabel.setText(title)

    def setBody(self, body: str):
        self.bodyLabel.setText(body)


class PodcastGalleryWidget(QWidget):
    clicked = Signal(bool)

    def __init__(self, parent=None):
        super().__init__(parent)
        self.verticalLayout = QVBoxLayout(self)
        self.horizontalLayout = QHBoxLayout()
        self.scrollArea = SingleDirectionScrollArea(self, Qt.Orientation.Horizontal)
        self.view = QWidget(self)
        self.viewLayout = QHBoxLayout(self.view)

        self.titleLabel = QLabel("精选播客", self)
        self.moreButton = HyperlinkLabel("查看更多", self)
        self.__initWidgets()

    def __initWidgets(self):
        self.scrollArea.setWidget(self.view)
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)

        self.titleLabel.setObjectName("titleLabel")
        self.titleLabel.setAlignment(Qt.AlignmentFlag.AlignLeft)
        self.moreButton.setObjectName("moreButton")
        self.moreButton.setFlat(True)
        self.verticalLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.verticalLayout.setSpacing(10)
        self.horizontalLayout.addWidget(self.titleLabel, 0, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
        self.horizontalLayout.addWidget(self.moreButton, 0, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.verticalLayout.addWidget(self.scrollArea)
        self.viewLayout.setContentsMargins(0, 0, 0, 0)
        self.viewLayout.setSpacing(8)
        self.viewLayout.setAlignment(Qt.AlignmentFlag.AlignLeft)

        StyleSheet.Card.apply(self)
        self.moreButton.clicked.connect(self.clicked)

    def addSinger(self, pixmap: str, singer: str, clicked=None):
        card = SingerCard(pixmap, singer)
        if clicked:
            card.clicked.connect(clicked)
        self.addWidget(card)

    def addCard(self, pixmap: str, title: str, body: str, clicked=None):
        card = TypeCard(pixmap, title, body)
        if clicked:
            card.clicked.connect(clicked)
        self.addWidget(card)

    def addGridWidget(self, title: str, body: str, pixmaps: list):
        card = GridWidgetCard(title, body, self)
        card.addImages(pixmaps)
        self.addWidget(card)

    def setTitle(self, title: str):
        self.titleLabel.setText(title)

    def addWidget(self, widget: QWidget):
        self.viewLayout.addWidget(widget)


class PlaylistGalleryWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.verticalLayout = QVBoxLayout(self)
        self.horizontalLayout = QHBoxLayout()
        self.flowLayout = FlowLayout()

        self.titleLabel = QLabel("全局播放列表", self)
        self.moreButton = HyperlinkLabel("查看更多", self)
        self.titleLabel.setObjectName("titleLabel")
        self.flowLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.verticalLayout.addLayout(self.flowLayout)
        self.verticalLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.horizontalLayout.addWidget(self.titleLabel, 0, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
        self.horizontalLayout.addWidget(self.moreButton, 0, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        StyleSheet.Card.apply(self)

    def addGridWidget(self, title: str, body: str, pixmaps: list):
        card = GridWidgetCard(title, body, self)
        card.addImages(pixmaps)
        self.addWidget(card)

    def addWidget(self, widget: QWidget):
        self.flowLayout.addWidget(widget)
